/**
 * Created by DevilsEyes on 2016/2/13.
 */
'use strict';

var express = require('express');
var router = express.Router();

var $notice = require('../midware/notice');
var $session = require('../midware/session');
var $file = require('../midware/file');

let filter = (h)=>({
    _id: h._id,
    title: h.title,//标题
    class: h.class,//班级
    article: h.article || '',//描述
    created: h.created//发布时间
});

//获取公告列表
router.get('/list', function (req, res, next) {

    let ssid = req.cookies.ssid;
    if (ssid === undefined) {
        return res.json({
            code: 9999,
            msg: '未登录'
        });
    } else {
        var Se = $session.getBySid(ssid);
        if (Se.role >= 999) {
            $notice
                .getList()
                .then((notices)=> {

                    res.json({
                        code: 0,
                        count: notices.length,
                        list: notices.map((n)=>filter(n))
                    });

                });
        } else {
            $notice
                .getList(Se.class)
                .then((notices)=> {

                    res.json({
                        code: 0,
                        count: notices.length,
                        list: notices.map((n)=>filter(n))
                    });

                });
        }
    }
});

module.exports = router;